#include "ByteBuffer.h"

ByteBuffer::ByteBuffer( unsigned int size )
	: m_size( size ) 
	, m_read( 0 ) 
	, m_write( 0 )
{
	m_buffer.reserve( m_size );
	m_buffer.resize( m_size );
}

ByteBuffer::~ByteBuffer()
{
	m_buffer.clear();
}

void ByteBuffer::Resize( unsigned int size ) 
{
#ifdef max
#undef max
#endif
	if( m_size < size  )
		size = std::max( size, 3 * m_size / 2 );
	else
		size = m_size;

	m_write -= m_read;
	m_buffer.reserve( size );
	m_buffer.resize( size );
	memmove( &m_buffer[0], Data(), m_write );

	m_read = 0;
	m_size  = size;
}

void ByteBuffer::Shift( unsigned int size )
{
	if( Size() < size )
		return;

	m_write = Size() - size;
	memmove( &m_buffer[0], &m_buffer[m_read + size], m_write );
	m_read = 0;
}
